package com.hdc.walnut.common.utils.CRUD;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

@Component
public class DataDelete {
    @Autowired
    private MysqlUtils mysqlUtils;

    public Map<String,Object> delete(String tabName, String titles){
        Map<String,Object> param = new HashMap<String, Object>();
        Connection connection = mysqlUtils.getConnection();
        Statement stm = null;
        try{
            stm = connection.createStatement();
            String  sqlStr = String.format("delete from  %s  %s",tabName,titles);
            System.out.println("[SQL]:"+sqlStr);
            int rows = stm.executeUpdate(sqlStr);
            if(rows>0){
                param.put("code","200");
                param.put("msg","success");
            }else{
                param.put("code","200");
                param.put("msg","fail");
            }
            stm.close();
        }catch(SQLException e){
            throw  new RuntimeException(e.getMessage());
        }finally {
            mysqlUtils.release(connection,stm);
        }
        return param;
    }
}
